﻿CREATE PROCEDURE [acms].[TemplateControl_Put]
	@Id int, 
	@TemplateId int,
	@Name varchar(50),
	@TypeId int,
	@VersionId timestamp,
	@IdOut int output,
	@TypeNameOut varchar(50) output,
	@AdminControlTypeOut varchar(255) output,
	@VersionIdOut timestamp output
AS
	if exists (select null from [TemplateControl_View] where Id = @Id) begin
		update [TemplateControls]
		set
			TemplateId = @TemplateId,
			Name = @Name,
			TypeId = @TypeId
		where
			Id = @Id
			and VersionId = @VersionId
			
		if @@Rowcount = 0
			return -100
		else
			set @IdOut = @Id
	end
	else if (@Id is null or @Id <= 0) begin
		insert into [TemplateControls]
			(TemplateId, Name, TypeId)
		values
			(@TemplateId, @Name, @TypeId)
		set @IdOut = @@Identity
	end
	else
		return -200
	
	select @TypeNameOut = TypeName, @AdminControlTypeOut = AdminControlType, @VersionIdOut = VersionId 
	From [TemplateControl_View] where Id = @Id
	
	
RETURN 0;